/****************************************************************************************************
* Title: Workhorses of Opportunity, Howard and Weinstein
*
* Inputs:
*   - justnormasylum.dta
*   - county_outcomes_dta.dta
*   - online_table4-2-1.dta
*
* Outputs:
*   - causal_college_income_and_married_p75.tex
*   - causal_income_p75.tex
*   - causal_income_appendix_p75.tex
*   - causal_college_p75.tex
*   - causal_college_appendix_p75.tex
*   - causal_married_p75.tex
*   - causal_married_appendix_p75.tex
*
* Description:
*   This script estimates effects of living in a "normal" school county on observed and causal 
*   outcomes for income, college attendance, and marriage. It computes weighted and unweighted 
*   regressions, generates LaTeX tables, and applies interpretation metadata.
****************************************************************************************************/

clear all
set maxvar 15000

* Load and prepare normal school flag
clear
use "justnormasylum"
rename hasnormalschool hasnormal
keep hasnormal cty_fips 
tempfile normal
save `normal'

* Merge normal school flag with county outcome data
clear
use county_outcomes_dta
gen cty_fips = state*1000 + county
rename state statefips
rename county countychetty
merge 1:m cty_fips using `normal', keep(2 3) nogen
save `normal', replace

* Merge in observational and causal measures
clear
use online_table4-2-1.dta
gen cty_fips = cty1990
drop county_name cz cz_name cz_pop2000 statename state_id stateabbrv
merge 1:m cty_fips using `normal', keep(2 3) nogen

*******************************************
* SECTION: Income Outcome
*******************************************
local causalname = "kr26"
local obsname = "kfr"

gen `obsname'_pooled_pooled_p75__t100 = `obsname'_pooled_pooled_p75 * 100

reghdfe `obsname'_pooled_pooled_p75__t100 hasnormal, absorb(state) cluster(state) nocon
eststo observ_income

gen precision_weight_`causalname' = 1 / causal_p75_cz_cty_`causalname'_se^2

reghdfe `obsname'_pooled_pooled_p75__t100 hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo observ_income_weighted

reghdfe causal_p75_cz_cty_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo causal_income_unweighted

reghdfe causal_p75_cz_cty_`causalname' hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo causal_income

reghdfe perm_res_p75_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo perm_res_income

*******************************************
* SECTION: College Outcome
*******************************************
local causalname = "c1823"
local obsname = "somecoll"

gen `obsname'_pooled_pooled_p75__t100 = `obsname'_pooled_pooled_p75 * 100

reghdfe `obsname'_pooled_pooled_p75__t100 hasnormal, absorb(state) cluster(state) nocon
eststo observ_college

gen precision_weight_`causalname' = 1 / causal_p75_cz_cty_`causalname'_se^2

reghdfe `obsname'_pooled_pooled_p75__t100 hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo observ_college_weighted

reghdfe causal_p75_cz_cty_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo causal_college_unweighted

reghdfe causal_p75_cz_cty_`causalname' hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo causal_college

reghdfe perm_res_p75_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo perm_res_college

*******************************************
* SECTION: Marriage Outcome
*******************************************
local causalname = "km26"
local obsname = "married"

gen `obsname'_pooled_pooled_p75__t100 = `obsname'_pooled_pooled_p75 * 100

reghdfe `obsname'_pooled_pooled_p75__t100 hasnormal, absorb(state) cluster(state) nocon
eststo observ_married

gen precision_weight_`causalname' = 1 / causal_p75_cz_cty_`causalname'_se^2

reghdfe `obsname'_pooled_pooled_p75__t100 hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo observ_married_weighted

reghdfe causal_p75_cz_cty_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo causal_married_unweighted

reghdfe causal_p75_cz_cty_`causalname' hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo causal_married

reghdfe perm_res_p75_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo perm_res_married

*******************************************
* Add Metadata for Tables
*******************************************
estadd local weighted="Unweighted" : observ_married observ_college observ_income causal_married_unweighted causal_college_unweighted causal_income_unweighted perm_res_college perm_res_married perm_res_income
estadd local interpretation="Effect on Place": observ_married observ_college observ_income observ_college_weighted observ_married_weighted observ_income_weighted perm_res_married perm_res_college perm_res_income 
estadd local scale="Per Childhood": observ_married observ_college observ_income perm_res_college perm_res_married perm_res_income observ_married_weighted observ_college_weighted observ_income_weighted


estadd local weighted="Precision Weights": causal_married causal_college causal_income observ_married_weighted observ_college_weighted observ_income_weighted
estadd local interpretation="Effect on Person": causal_married causal_college causal_income causal_married_unweighted causal_college_unweighted causal_income_unweighted
estadd local scale="Per Year": causal_married causal_college causal_income causal_married_unweighted causal_college_unweighted causal_income_unweighted


*******************************************
* Variable Labels for Tables
*******************************************
label var hasnormal "Normal"
label var kfr_pooled_pooled_p75__t100 "\shortstack{Family Income  \\ Percentile, 2014-15}"
label var somecoll_pooled_pooled_p75__t100 "\shortstack{Some College \\ Age 25+}"
label var married_pooled_pooled_p75__t100 "\shortstack{Married \\ 2015}"

label var causal_p75_cz_cty_kr26 "\shortstack{Family Income \\  Percentile, Age 26}"
label var causal_p75_cz_cty_c1823 "\shortstack{Attended College \\ Age 18-23}"
label var causal_p75_cz_cty_km26 "\shortstack{Married \\ Age 26}"

label var perm_res_p75_kr26 "\shortstack{Family Income \\  Percentile, Age 26}"
label var perm_res_p75_c1823 "\shortstack{Attended College \\ Age 18-23}"
label var perm_res_p75_km26 "\shortstack{Married \\ Age 26}"

*******************************************
* Output TeX Tables
*******************************************
esttab observ_college causal_college observ_income causal_income observ_married causal_married, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college causal_college observ_income causal_income observ_married causal_married using ///
    "causal_college_income_and_married_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote scalar("weighted Weights" "scale Scale" "interpretation Interpretation") tex replace

esttab observ_income observ_income_weighted perm_res_income causal_income_unweighted causal_income, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote replace ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_married observ_married_weighted perm_res_married causal_married_unweighted causal_married, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote replace ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college observ_college_weighted perm_res_college causal_college_unweighted causal_college, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote replace ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

* Export select outcome tables individually
esttab observ_income causal_income using "causal_income_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_income observ_income_weighted perm_res_income causal_income_unweighted causal_income using ///
    "causal_income_appendix_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college causal_college using "causal_college_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college observ_college_weighted perm_res_college causal_college_unweighted causal_college using ///
    "causal_college_appendix_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_married causal_married using "causal_married_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_married observ_married_weighted perm_res_married causal_married_unweighted causal_married using ///
    "causal_married_appendix_p75.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")
